<?php
namespace App\Common\Utill;

use FFMpeg\FFMpeg;
use FFMpeg\FFProbe;
use FFMpeg\Format\Video\X264;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Coordinate\TimeCode;

class FFMpegUtil
{
    protected  $path;

    public function __construct()
    {
        $this->path = [
            'ffmpeg.binaries' => '/usr/local/Cellar/ffmpeg/4.3.1_4/bin/ffmpeg',
            'ffprobe.binaries' => '/usr/local/Cellar/ffmpeg/4.3.1_4/bin/ffprobe',
        ];
    }

    /**
     * 视频详情
     * @param $file
     * @return FFProbe\DataMapping\Format
     */
    public function getVideoInfo($file){
        $ffprobe = FFProbe::create($this->path);
        $videoInfo = $ffprobe->format($file);#视频详情
        return  $videoInfo;
    }

    /**
     * 视频大小（字节）
     * @param $file
     * @return mixed
     */
    public function getVideoSize($file){
        $ffprobe = FFProbe::create($this->path);
        $sie = $ffprobe->format($file)->get('size',100);
        return  $sie;
    }

    /**
     * 视频时长
     * @param $file
     * @return mixed
     */
    public function getVideoDuration($file){
        $ffprobe = FFProbe::create($this->path);
        $duration = $ffprobe->format($file)->get('duration',100);
        return  $duration;
    }

    /**
     * 获取视频描述截图
     * @param $file
     * @param $ttl
     * @return string
     */
    public function getVideoJpg($file,$ttl,$file_name)
    {
        $ffmpeg = FFMpeg::create($this->path);
        $video = $ffmpeg->open($file);
        $frame = $video->frame(TimeCode::fromSeconds($ttl));

        $filePush = 'video/' . $file_name;
        if (!is_dir(storage_path("video"))) {
            mkdir(storage_path("video"), 0777);
        }
        //保存jpg图片
        $frame->save(storage_path($filePush));
        return $filePush;

    }

    /**
     * 获取视频gif截图
     * @param $file
     * @return string
     */
    public function getVideoGif($file)
    {
        $realPath = $file->getRealPath();
        $file_name = $file -> getClientOriginalName();
        $video = self::$ffmpeg->open($realPath);

        $filePush = 'video/' . $file_name . '.gif';
        if (!is_dir(storage_path("video"))) {
            mkdir(storage_path("video"), 0777);
        }
        //保存gif动态图
        $frame = $video->gif(TimeCode::fromSeconds(1), new Dimension(400, 200), 1)->save(storage_path($filePush));
        return $filePush;
    }

    /**
     * 视频水印
     * @param $file
     * @return \FFMpeg\Media\Audio|\FFMpeg\Media\Video
     */
    public function watermark($file)
    {
        $ffmpeg = FFMpeg::create($this->path);
        $video = $ffmpeg->open($file);

        $watermarkPath = storage_path('img.png');

        $relative = [
            'position' => 'relative',//相对位置
            //'position' => 'absolute',//绝对位置
            'bottom' => 50,
            'right' => 50
        ];
        $format = new X264('aac');
        $video->filters()->watermark($watermarkPath, $relative);
        return $video->save($format, storage_path('export.mp4'));

    }
}

